package chaw.android.util;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


public class ImageUpload {
	private String lineEnd = "\r\n";
	private String twoHyphens = "--";
	private String boundary = "*****";
	private FileInputStream mFileInputStream;
	private URL connectUrl;
	private DataOutputStream dos;
	private HttpURLConnection conn;
	private int bytesAvailable, bytesRead;
	private int maxBufferSize, bufferSize;
	private byte[] buffer;
	  
	public ImageUpload(String urlString){
		try {   
		  connectUrl = new URL(urlString);
	   
		  conn = (HttpURLConnection)connectUrl.openConnection();   
		  conn.setDoInput(true);
		  conn.setDoOutput(true);
		  conn.setUseCaches(false);
		  conn.setRequestMethod("POST");
		  conn.setRequestProperty("Connection", "Keep-Alive");
		  conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded" ) ;
		  conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
	   // write data
		  dos = new DataOutputStream(conn.getOutputStream());

	   
	   } catch (Exception e) {
//			  Log.d("Test", "exception " + e.getMessage());
			  // TODO: handle exception
		} 
		

	}
		
	public String getResponse(){
		   // get response
		String response = "";
		try{
			int ch;
			InputStream is = conn.getInputStream();
			StringBuffer b = new StringBuffer();
			while( ( ch = is.read() ) != -1 ){
				b.append( (char)ch );	
			}
			response=b.toString(); 
//			Log.e("Test", "result = " + response);
			dos.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return response;
	}
	
	public void addData(String param, String value){
		try{
			dos.writeBytes( twoHyphens + boundary + lineEnd ) ; //필드 구분자 시작
			dos.writeBytes( "Content-Disposition: form-data; name=\"");
			dos.write( param.getBytes("utf-8") );
			dos.writeBytes("\""+ lineEnd ) ;
			dos.writeBytes( lineEnd ) ;
			dos.write( value.getBytes("utf-8") ) ;
			dos.writeBytes( lineEnd ) ;
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void upItem(File fileName, String imageName, String user_id, String imageType, String imageUrl){
		try{
		  mFileInputStream = new FileInputStream(fileName);
//		  Log.d("Test", "mFileInputStream  is " + mFileInputStream);
		  dos.writeBytes(twoHyphens + boundary + lineEnd);
		  dos.writeBytes("Content-Disposition: form-data; name=\""+imageName+"\";filename=\"" + user_id + "_" + imageUrl + 
				  			imageType +".jpg"+"\"" + lineEnd);
		  dos.writeBytes(lineEnd);
		  bytesAvailable = mFileInputStream.available();
		  maxBufferSize = 1024;
		  bufferSize = Math.min(bytesAvailable, maxBufferSize);
		  buffer = new byte[bufferSize];
		  bytesRead = mFileInputStream.read(buffer, 0, bufferSize);
	   
		  while (bytesRead > 0) {
			  dos.write(buffer, 0, bufferSize);
			  bytesAvailable = mFileInputStream.available();
			  bufferSize = Math.min(bytesAvailable, maxBufferSize);
			  bytesRead = mFileInputStream.read(buffer, 0, bufferSize);
		  } 
	   
		  dos.writeBytes(lineEnd);
		  dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
	   
	   // close streams
//		  Log.e("Test" , "File is written");
//		  Log.d("Test", "image byte is " + bytesRead);
		  mFileInputStream.close();
		  dos.flush(); // finish upload...   
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

}
